import QtQuick 2.15
import QtQuick.Controls 2.15
import QtLocation 5.11
import "./../../Control/RadioJs.js" as RadioJs
import RadioGlobal 1.0 as RadioGlobal
import "./Dialog"

Item {
    id:footItem


    //创建一个键值对类型
    property var collectMap : ({})

    //添加元素
    function addCollectMap(key, value) {
        footItem.collectMap[key] = value;
        console.log("Added element:", key, value);
    }

    //删除元素
    function removeCollectMap(key) {
        if (footItem.collectMap.hasOwnProperty(key)) {
            delete footItem.collectMap[key];
            console.log("Removed element:", key);
        } else {
            console.log("Element not found:", key);
        }
    }

    //获取大小
    function sizeCollectMap(){
        return Object.keys(footItem.collectMap).length
    }

    //遍历Map
    function saveCollectMap(){
        //{"fm_ch":[{"fav":0,"freq":9600},{"fav":0,"freq":10380},{"fav":0,"freq":10800}],"index":1}
        var recordIndex = 0
        var currentIndex = searchChannelView.currentIndex.toString()

        var strJoiner = ""
        strJoiner += "{"
        strJoiner+="\"fm_ch\":"
        strJoiner += "["
        //拼接数据
        var keys = Object.keys(footItem.collectMap)
        for( var i = 0 ; i< keys.length ; i++){
            var key = keys[i]
            strJoiner+="{"
            strJoiner+="\"fav\":"
            strJoiner+=0
            strJoiner+=","
            strJoiner+="\"freq\":"
            strJoiner+=footItem.collectMap[key]
            strJoiner+="}"
            if(i < keys.length - 1)
                strJoiner += ","

            if(key ===  currentIndex)
            {
                recordIndex = i
            }
        }
        strJoiner += "],"
        strJoiner += "\"index\":"
        strJoiner += recordIndex
        strJoiner += "}"

        //序列化至Json文件
        jsonHelper.serializeData(strJoiner)
    }

    function clearCollectMap(){
        Object.keys(footItem.collectMap).forEach(key=>delete footItem.collectMap[key])
        console.log(footItem.sizeCollectMap())
    }

    //定义限制对话框
    CWarningDialog{
        id: warningDialog
    }



    //顶部 Label
    Item {
        id: currentChannelItem
        anchors.top: parent.top
        width: parent.width
        height: parent.height * 0.05
        Label{
            anchors.centerIn: parent
            text: RadioGlobal.RadioData.searchChannelData.get( searchChannelView.currentIndex) !== undefined
                  ? "当前播放的频道: " + RadioGlobal.RadioData.searchChannelData.get( searchChannelView.currentIndex ).freq
                  : "当前未播放频道"
        }
    }

    //显示按钮
    ListView{
        id: searchChannelView
        anchors.top: currentChannelItem.bottom
        width: parent.width
        height: parent.height * 0.85
        model:RadioGlobal.RadioData.searchChannelData
        delegate: Rectangle{
            id: delegateItem
            width: searchChannelView.width
            height: 20
            border.color: "black"
            radius: 5
            color: searchChannelView.currentIndex === index ? "red" : "white"
            Text {
                id: idxText
                anchors.left: parent.left
                width: parent.width * 0.9
                height: parent.height
                text: model.freq
                horizontalAlignment: Text.AlignHCenter // 水平居中
                verticalAlignment: Text.AlignVCenter // 垂直居中
                MouseArea{
                    id: idxMouse
                    anchors.fill: parent
                    onClicked: {
                        searchChannelView.currentIndex = index
                    }
                }
            }
            //收藏按钮
            Button{
                id: collectBtn
                anchors.left: idxText.right
                width: parent.width * 0.1
                height: parent.height
                text: ! collectBtn.checked ? "collect" : "uncollect"
                checked: false   //检测点击状态
                background: Rectangle {
                    color: collectBtn.checked  ? "green" : "white" // 设置背景颜色
                    border.color: "darkblue" // 设置边框颜色
                }

                onClicked: {
                    if( ! collectBtn.checked ) //收藏
                    {
                        var colloctSize = footItem.sizeCollectMap()
                        if( colloctSize >= 5 )
                        {
                            warningDialog.collectLimitDlg.open()
                            return
                        }
                        console.log(" 收藏: " , index )
                        var freqData = RadioGlobal.RadioData.searchChannelData.get(index)
                        footItem.addCollectMap( index , freqData.freq )
                        footItem.saveCollectMap()

                    }
                    else //取消收藏
                    {
                        console.log(" 取消收藏: " , index )
                        footItem.removeCollectMap(index)
                        footItem.saveCollectMap()

                    }
                    //更改收藏标记
                    collectBtn.checked = !collectBtn.checked
                }
            }
        }

        //播放逻辑
        onCurrentIndexChanged: {
            if( searchChannelView.currentIndex === -1)
                return

            var channelData = RadioGlobal.RadioData.searchChannelData.get(searchChannelView.currentIndex)
            RadioJs.playChannel( channelData.freq )
        }
    }

    //搜索按钮
    Item{
        id: btnItem
        anchors.top: searchChannelView.bottom
        width: parent.width
        height: parent.height * 0.1
        Button{
            id: searchChannelBtn
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Search"
            onClicked: {
                //清空收藏数据
                footItem.clearCollectMap()

                RadioJs.stopChannel()
                //再次搜索
                RadioJs.searchChannel()
            }
        }
    }

}
